package com.aiti.lulian.service.impl;

import cn.hutool.core.date.DateTime;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.lulian.dto.CardInquiryDto;
import com.aiti.lulian.entity.CardEnterpriseUserData;
import com.aiti.lulian.entity.crad.CardInquiry;
import com.aiti.lulian.mapper.CardEnterpriseUserDataMapper;
import com.aiti.lulian.mapper.CardInquiryMapper;
import com.aiti.lulian.service.ICardInquiryService;
import com.aiti.lulian.utils.DateFilterUtil;
import com.aiti.lulian.vo.CardInquiryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class CardInquiryServiceImpl extends BaseSerivceImpl<CardInquiryMapper, CardInquiry> implements ICardInquiryService {

    private CardInquiryMapper cardInquiryMapper;

    public CardInquiryServiceImpl(CardInquiryMapper cardInquiryMapper) {
        this.cardInquiryMapper = cardInquiryMapper;
    }

    /**
     * 查询当前用户的询盆列表
     *
     * @param dto
     * @return
     */
    @Override
    public List<CardInquiryVo> getInquiryList(Page page, CardInquiryDto dto) {
//        LambdaQueryWrapper<CardInquiry> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(StringUtils.isNotBlank(dto.getCradUserId()),CardInquiry::getCradUserId,dto.getCradUserId());
//        lqw.eq(CardInquiry::getIsDelete,"0");
//        String dateFilterType = dto.getDateFilterType();
//        if(!StringUtils.isEmpty(dateFilterType)){
//            Map<String, DateTime> beginAndEnd = DateFilterUtil.getBeginAndEnd(dateFilterType);
//            DateTime begin = beginAndEnd.get("begin");
//            DateTime end = beginAndEnd.get("end");
//            lqw.ge(CardInquiry::getBaseCreateTime,begin);
//            lqw.le(CardInquiry::getBaseCreateTime,end);
//        }
//
//        Page resultPage = cardInquiryMapper.selectPage(page, lqw);
//        List records = resultPage.getRecords();
        String dateFilterType = dto.getDateFilterType();
        if(!StringUtils.isEmpty(dateFilterType)){
            Map<String, DateTime> beginAndEnd = DateFilterUtil.getBeginAndEnd(dateFilterType);
            DateTime begin = beginAndEnd.get("begin");
            DateTime end = beginAndEnd.get("end");
            dto.setBeginDate(begin);
            dto.setEndDate(end);
        }
        List<CardInquiryVo> cardInquiryVos = cardInquiryMapper.inquiryPage(page, dto);
        return cardInquiryVos;
    }

    @Override
    public CardInquiry getInquiryDetailInfo(CardInquiry cardInquiry) {
        return cardInquiryMapper.selectById(cardInquiry.getBaseId());
    }

    @Override
    public Page getInquiryPage(Page page, CardInquiryDto dto,String userId) {
        LambdaQueryWrapper<CardInquiry> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CardInquiry::getIsDelete,"0");
        //查询发出名片人的条件


        lqw.eq(CardInquiry::getCradUserId,userId);
        lqw.eq(org.apache.commons.lang3.StringUtils.isNotBlank(dto.getLinkMan()),CardInquiry::getLinkMan,dto.getLinkMan());
        lqw.eq(org.apache.commons.lang3.StringUtils.isNotBlank(dto.getLinkPhone()),CardInquiry::getLinkPhone,dto.getLinkPhone());
        Page<CardInquiry> cardInquiryPage = cardInquiryMapper.selectPage(page, lqw);

        return cardInquiryPage;
    }
}
